package com.hydra.baseui.recyclerlist;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

/**
 * Created by Administrator on 2016/7/12.
 *
 */
class RecyclerEndlessAdapter extends RecyclerHeaderListAdapter {

	private View mEndlessView;

	private EndlessListener mEndlessListener;

    private int mEndlessViewHeight;

    private boolean mIsEndlessAttach = false;

	RecyclerEndlessAdapter(Adapter adapter) {
		super(adapter);
	}

	@Override
	public void addFooterView(View view) {
        int footersCount = getFootersCount();
        addFooterView(mEndlessView != null ? footersCount - 1 : footersCount, view);
	}

    @Override
    public void addFooterView(int index, View view) {
        if(mEndlessView != null && index == getFootersCount()) {
            throw new IndexOutOfBoundsException("the last footer should always be endless view");
        }
        super.addFooterView(index, view);
    }

    @Override
    public void onViewAttachedToWindow(ViewHolder holder) {
        super.onViewAttachedToWindow(holder);

        if(holder.itemView == mEndlessView) {
            mIsEndlessAttach = true;

            handleEndlessView();
        }
    }

    @Override
    public void onViewDetachedFromWindow(ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);

        if(holder.itemView == mEndlessView) {
            mIsEndlessAttach = false;
        }
    }

    public void setEndlessView(@NonNull View view) {
        removeEndlessView();

        mEndlessView = view;

        //TODO 可能会不准
        UIHelper.measureView(view);

        mEndlessViewHeight = view.getMeasuredHeight();

        super.addFooterView(getFootersCount(), view);
	}

	public void removeEndlessView() {
		if(mEndlessView != null) {
            removeFooterView(mEndlessView);

			mEndlessView = null;
		}
	}

    public View getEndlessView() {
        return mEndlessView;
    }

    public void setEndlessListener(EndlessListener endlessListener) {
        mEndlessListener = endlessListener;
    }

    void notifyEndless() {
        if(mEndlessView != null && mIsEndlessAttach) {
            handleEndlessView();
        }
    }

	private void handleEndlessView() {
        if(hasMore()) {
            showEndlessView();

            if(mEndlessListener.loadMoreWhenEndlessShow()) {
                mEndlessListener.loadMore();
            }
        } else {
            hideEndlessView();
        }
    }

    private void showEndlessView() {
        RecyclerView.LayoutParams rl = RecyclerView.LayoutParams.class.cast(mEndlessView.getLayoutParams());

        if(rl != null) {
            mEndlessView.setVisibility(View.VISIBLE);

            if(rl.height != mEndlessViewHeight) {
                rl.height = mEndlessViewHeight;

                mEndlessView.requestLayout();
            }
        }
    }

	private void hideEndlessView() {
        RecyclerView.LayoutParams rl = RecyclerView.LayoutParams.class.cast(mEndlessView.getLayoutParams());

        if(rl != null) {
            mEndlessView.setVisibility(View.GONE);

            if(rl.height != 0) {
                rl.height = 0;

                mEndlessView.requestLayout();
            }
        }
    }

    private boolean hasMore() {
        return mEndlessView != null && mEndlessListener != null && mEndlessListener.hasMoreData();
    }

	@Override
	public void release() {
		super.release();

		mEndlessView = null;

		mEndlessListener = null;
	}
}
